using System.Web.Mvc;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Attributes;
using good1.Web.Controllers.Infrastructure;
using good1.Web.Controllers.Infrastructure.Cookies;
using SharpArch.Web.NHibernate;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    public class CharitiesController : BaseController
    {
        public CharitiesController(ICharityService charityService, ISecurityContextService securityContext) : base(securityContext)
        {
            Check.Require(charityService != null, "charityService may not be null");

            this.charityService = charityService;
        }

        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        public ActionResult Index() {
            return View(charityService.GetAllSorted());
        }

        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        public ActionResult Show(int id) {
            Charity charity = charityService.Get(id);
            return View(charity);
        }

        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        public ActionResult Create() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Charity charity) {
            if (charity.IsValid()) {
                charityService.SaveOrUpdate(charity);

                TempData["message"] = "The charity was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                charity.ValidationResults());
            return View();
        }

        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        public ActionResult Edit(int id) {
            Charity charity = charityService.Get(id);
            return View(charity);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] Charity charity) {
            Charity charityToUpdate = charityService.Get(id);

            TransferFormValuesTo(charityToUpdate, charity);

            if (charityToUpdate.IsValid()) {
                TempData["message"] = "The charity was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                charityService.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    charityToUpdate.ValidationResults());
                return View(charityToUpdate);
            }
        }

        private void TransferFormValuesTo(Charity charityToUpdate, Charity charityForm) {
            charityToUpdate.CharityName = charityForm.CharityName;
            charityToUpdate.Url = charityForm.Url;
            charityToUpdate.Description = charityForm.Description;
            charityToUpdate.TagLine = charityForm.TagLine;
            charityToUpdate.LogoUrl = charityForm.LogoUrl;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            TempData["Message"] = charityService.Delete(id);
            return RedirectToAction("Index");
        }

        private readonly ICharityService charityService;
    }
}
